'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
           if($state.includes('**.rechargeRule.update')){
                title="编辑充值规则";
                var id = $state.params.id;
                activate(id);
                validate(id);
            }else if($state.includes('**.rechargeRule.create')){
                title="添加充值规则";
                validate(null);

            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            $scope.submit=function() {

                var rechargeMoney=$('#rechargeMoney').val();
                var sendMoney=$('#sendMoney').val();
                var minMoney=$('#minMoney').val();
                var maxMoney=$('#maxMoney').val();
                var sort=$('#sort').val();
                var rate=$('#rate').val();

                if(rate>1){
                    $scope.alertModal({"content": "比率必须是数字,,并且大于等于0,小于等于1"});
                    return;
                }

                var m = $scope.record;
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url: $scope.record.ruleId==undefined? $scope.app.project_name + '/rechargeRule/add/addRechargeRule':$scope.app.project_name + '/rechargeRule/update/updateRechargeRule',
                        data: m
                    }).then(function (result) {
                        if (result.httpCode == 200) {
                            if (result.data > 0) {
                                $scope.alertModal({"content": "操作成功"});
                            } else {
                                $scope.alertModal({"content": "操作失败"});
                            }
                        } else {
                            $scope.alertModal({"content": "操作失败"});
                        }
                        $scope.isDisabled = false;
                        $scope.loading = false;
                        $state.go('main.biz.rechargeRule.list');
                        $scope.$apply();

                    });
            }

            //表单验证
            function validate(userId){
                $.validator.addMethod('notEqual', function(value, ele){
                    return value != this.settings.rules[ele.name].notEqual;
                });
                jQuery('form').validate({
                    rules: {
                        rechargeMoney: {
                            required: true,
                            maxLengthB:[10]
                        },
                        sendMoney: {
                            required: true
                        },
                        rate: {
                            required: true
                        },
                        minMoney:{
                            required: true
                        },
                        maxMoney:{
                             required: true
                        },
                        // sort:{
                        //     required: true
                        // },
                        showText:{
                            required: true
                        }


                    },
                    messages: {
                        rechargeMoney: {
                            required: '充值金额不能为空'
                        },
                        sendMoney: {
                            required: '赠送金额不能为空'
                        },
                        rate: {
                            required: '比率不能为空'
                        },
                        minMoney:{
                            required: '最低金额不能为空'
                        },
                        maxMoney:{
                            required: '最高金额不能为空'
                        },
                        // sort:{
                        //     required: '排序不能为空'
                        // },
                        showText:{
                            required: '显示文本不能为空'
                        }
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }


            // 初始化页面
            function activate(id) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/rechargeRule/read/detail',
                    data: {'ruleId': id}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }


        }]);